﻿using System.Collections;
using System.Collections.Generic;


/// <summary>
/// 协程封装
/// </summary>
public class UnitCoroutine : MonoSingletonBase<UnitCoroutine>
{ 
    /// <summary>
    /// 
    /// </summary>
    public static Dictionary<string, UnitCoroutineDetail> DetailLibs { get; private set; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="name"></param>
    /// <param name="IEnumerator"></param>
    public static void Run(string name, IEnumerator @IEnumerator)
    {
        bool isFind = DetailLibs.TryGetValue(name, out UnitCoroutineDetail output);
        if (isFind)
        {
            if (output.State == State.Running) return;

            Instance.StartCoroutine(@IEnumerator);
            output.State = State.Running;

            return;
        }


        var @new = new UnitCoroutineDetail(name, @IEnumerator);
        DetailLibs.TryAdd(name, @new);
        Instance.StartCoroutine(@IEnumerator);
    }
}